import sun.reflect.generics.tree.Tree;

import java.util.*;

public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //public TreeNode root;//将来指定根节点

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    public List<TreeNode> preOrder2(TreeNode root) {
        List<TreeNode> ret = new LinkedList<>();
        if (root == null) {
            return ret;
        }
        ret.add(root);
        List<TreeNode> leftTree = preOrder2(root.left);
        ret.addAll(leftTree);
        List<TreeNode> rightTree = preOrder2(root.right);
        ret.addAll(rightTree);
        return ret;
    }

    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    public List<TreeNode> inOrder2(TreeNode root) {
        List<TreeNode> ret = new LinkedList<>();
        if (root == null) {
            return ret;
        }
        List<TreeNode> leftTree = inOrder2(root.left);
        ret.addAll(leftTree);
        ret.add(root);
        List<TreeNode> rightTree = inOrder2(root.right);
        ret.addAll(rightTree);
        return ret;
    }

    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    public List<TreeNode> postOrder2(TreeNode root) {
        List<TreeNode> ret = new LinkedList<>();
        if (root == null) {
            return ret;
        }
        List<TreeNode> leftTree = inOrder2(root.left);
        ret.addAll(leftTree);
        List<TreeNode> rightTree = inOrder2(root.right);
        ret.addAll(rightTree);
        ret.add(root);
        return ret;
    }

    /*public int size = 0;

    public int nodeSize(TreeNode root) {
        if (root == null) return 0;
        size++;
        nodeSize(root.left);
        nodeSize(root.right);
        return size;
    }*/
    public int nodeSize(TreeNode root) {
        if (root == null) return 0;
        return nodeSize(root.left) + nodeSize(root.right) + 1;
    }

    public int size = 0;

    public int getLeafSize(TreeNode root) {
        if (root == null) return 0;
        if (root.left == null && root.right == null) return 1;
        return getLeafSize(root.left) + getLeafSize(root.right);
    }

    public int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) return 0;
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) +
                getKLevelNodeCount(root.right, k - 1);
    }

    public int getHeight(TreeNode root) {
        if (root == null) return 0;

        return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
    }

    // 检测值为value的元素是否存在
    public TreeNode ret = null;

    public TreeNode find(TreeNode root, char val) {
        if (root == null) return null;
        if (root.val == val) {
            ret = root;
        }
        find(root.left, val);
        find(root.right, val);
        return ret;
    }

    //层序遍历
    public void levelOrder(TreeNode root) {
        if (root == null) return;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                queue.add(cur.left);
            }
            if (cur.right != null) {
                queue.add(cur.right);
            }
        }
    }

    public List<List<TreeNode>> levelOrder1(TreeNode root) {
        List<List<TreeNode>> ret = new ArrayList<>();
        if (root == null) return ret;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<TreeNode> tmp = new ArrayList<>();
            while (size != 0) {
                TreeNode cur = queue.poll();
                System.out.print(cur.val + " ");
                tmp.add(cur);
                size--;
                if (cur.left != null) {
                    queue.add(cur.left);
                }
                if (cur.right != null) {
                    queue.add(cur.right);
                }
            }
            ret.add(tmp);
        }
        return ret;
    }

    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        if (root == null)
            return true;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur != null) {
                queue.add(cur.left);
                queue.add(cur.right);
            } else {
                break;
            }
        }
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur != null) return false;
        }
        return true;
    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) return null;
        if (p == root || q == root) return root;
        TreeNode leftTree = lowestCommonAncestor(root.left, p, q);
        TreeNode rightTree = lowestCommonAncestor(root.right, p, q);
        if (leftTree != null && rightTree != null) {
            return root;
        } else if (leftTree == null) {
            return rightTree;
        } else {
            return leftTree;
        }
    }

    //236. 二叉树的最近公共祖先
    private boolean getPath(TreeNode root, TreeNode node, Stack<TreeNode> stack) {
        if (root == null || node == null) return false;
        stack.push(root);
        if (root == node) return true;
        boolean leftFlag = getPath(root.left, node, stack);
        if (leftFlag) {
            return true;
        }
        boolean rightFlag = getPath(root.right, node, stack);
        if (rightFlag) {
            return true;
        }
        stack.pop();
        return false;
    }

    public TreeNode lowestCommonAncestor1(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) return null;
        Stack<TreeNode> stackP = new Stack<>();
        Stack<TreeNode> stackQ = new Stack<>();
        getPath(root, p, stackP);
        getPath(root, q, stackQ);

        int sizeP = stackP.size();
        int sizeQ = stackQ.size();

        if (sizeP < sizeQ) {
            int size = sizeQ - sizeP;
            while (size > 0) {
                stackQ.pop();
                size--;
            }
        } else {
            int size = sizeP - sizeQ;
            while (size > 0) {
                stackP.pop();
                size--;
            }
        }
        while (!stackP.isEmpty() && !stackQ.isEmpty()) {
            TreeNode tmpP = stackP.pop();
            TreeNode tmpQ = stackQ.pop();
            if (tmpP == tmpQ) return tmpQ;
        }
        return null;
    }

    
}
